@echo off

REM 定义输出压缩包名称
set "ZIP_OUTPUT=rxdb_checker.zip"
set "TAR_GZ_OUTPUT=rxdb_checker.tar.gz"
set "TAR_FILE=rxdb_checker.tar"

REM 检查是否存在dist目录，如果不存在则创建,如果存在则删除
if exist "dist" (
    echo "dist directory exists, deleting..."
    rmdir /s /q "dist"
) else (
    mkdir dist
)

REM 删除已存在的压缩包
if exist "%ZIP_OUTPUT%" del "%ZIP_OUTPUT%"
if exist "%TAR_GZ_OUTPUT%" del "%TAR_GZ_OUTPUT%"
if exist "rxdb_checker.exe" del "rxdb_checker.exe"
if exist "rxdb_checker" del "rxdb_checker"

go env -w GOOS=linux
go build .
go env -w GOOS=windows
go build .

REM 尝试使用 7-Zip 打包为 .tar.gz
if exist "C:\Program Files\7-Zip\7z1.exe" (
    echo "use 7-Zip build in %TAR_GZ_OUTPUT% ..."
    "C:\Program Files\7-Zip\7z.exe" a -ttar %TAR_FILE% jre rxdb_checker.exe *.jar config.yml README.md rxdb_checker
    "C:\Program Files\7-Zip\7z.exe" a -tgzip %TAR_GZ_OUTPUT% %TAR_FILE%
    if exist "%TAR_FILE%" del "%TAR_FILE%"
    echo "build success: %TAR_GZ_OUTPUT%"
    move %TAR_GZ_OUTPUT% dist
) else (

    echo "use PowerShell build in  %ZIP_OUTPUT% ..."

    powershell.exe -Command "$files = @('jre', 'rxdb_checker.exe', '*.jar', 'config.yml', 'README.md', 'rxdb_checker'); $existingFiles = $files | Where-Object { Test-Path $_ }; Compress-Archive -Path $existingFiles -DestinationPath '%ZIP_OUTPUT%'" 

    echo "build success: %ZIP_OUTPUT%"
    move %ZIP_OUTPUT% dist
)